package qhLoading.info 
{
	
	import flash.display.*;
    import flash.geom.*;
    import flash.system.*;
	import qhLoading.QhLoadItem;
	/**
	 * ...
	 * @author gcxk721
	 */
	public class SwfInfo extends ResourceInfo 
	{
		
		private var _clip:MovieClip;
        private var _bitmapData:BitmapData;
        private var _loaderInfo:LoaderInfo;
        private var _appDomain:ApplicationDomain;

        public function SwfInfo(item : QhLoadItem = null)
        {
            super(item);
        }

        public function get clip() : MovieClip
        {
            return _clip;
        }
		
		/**
		 * 将第一帧转换成图片数据
		 */
        public function get bitmapData() : BitmapData
        {
            var rect : Rectangle = null;
            if (_bitmapData == null && _clip)
            {
                _clip.gotoAndStop(1);
                rect = _clip.getBounds(_clip);
                if (rect.height == 0 || rect.width == 0)
                {
					
                }
                else
                {
                    _bitmapData = new BitmapData(rect.width, rect.height, true, 0);
                    _bitmapData.draw(_clip, new Matrix(1, 0, 0, 1, -rect.x, -rect.y), null, null, null, false);
                    _clip = null;
                }
            }
            return _bitmapData;
        }

        override public function set data(value : Object) : void
        {
            super.data = value;
            if (value is MovieClip)
            {
                _clip = value as MovieClip;
                _appDomain = _clip.loaderInfo.applicationDomain;
            }
            else if (value is LoaderInfo)
            {
                _loaderInfo = value as LoaderInfo;
                _appDomain = _loaderInfo.applicationDomain;
                if (_loaderInfo.swfVersion >= 9)
                {
                    _clip = _loaderInfo.content as MovieClip;
                }
                else
                {
                    throw new Error(_loaderInfo.url + "version：" + _loaderInfo.swfVersion);
                }
            }
            if (_clip)
            {
                _clip.gotoAndStop(1);
            }
        }
		
		/**
		 * 取得加载内容中包含的类
		 * @param	clsName
		 * @return
		 */
        public function getAssetClass(clsName : String) : Class
        {
            if (_appDomain == null)
            {
                throw new Error("not initialized");
            }
            if (_appDomain.hasDefinition(clsName))
            {
                return _appDomain.getDefinition(clsName) as Class;
            }
            return null;
        }

		/**
		 * 通过类名 取得一个MovieClip
		 * @param	clsName
		 * @return
		 */
		public function getChildMovieClip(clsName : String) : MovieClip
		{
			var cls : Class = getAssetClass(clsName);
			if (cls != null)
			{
				var mc : MovieClip = new cls();
				mc.stop();
				return mc;
			}
			return null;
		}
		
        override public function dispose() : void
        {
            isLoaded = false;
            isLoading = false;
            _clip = null;
            _loaderInfo = null;
            _appDomain = null;
            if (_bitmapData)
            {
                _bitmapData.dispose();
                _bitmapData = null;
            }
            super.dispose();
        }

        override protected function unload(param1:LoaderInfo) : void
        {
            param1.loader.unloadAndStop();
            super.unload(param1);
        }
		
	}

}